//
//  BaseTabBar.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/2.
//

#import "BaseTabBar.h"
#import "Public.h"
#import "BaseTabBarButton.h"

@implementation BaseTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = COLOR_WHITE;
    }
    return self;
}

- (void)setModelArray:(NSMutableArray *)modelArray {
    _modelArray = modelArray;
    [self setupUI];
}

- (void)setupUI {
    //移除视图防止重复添加
    [[self subviews]makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    self.bgimView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 60)];
    self.bgimView.image = [UIImage imageNamed:@"barim"];
    [self addSubview:self.bgimView];
    [self sendSubviewToBack:self.bgimView];
    
    //布局按钮
    for (NSUInteger i = 0; i < self.modelArray.count; i++) {
        BaseTabBarModel *btnModel = self.modelArray[i];
        CGFloat x_spacing = (SCREEN_W - 60*self.modelArray.count)/5;
        BaseTabBarButton * TarBarButton = [BaseTabBarButton buttonWithType:(UIButtonTypeCustom)];
        TarBarButton.imageSize = i==2 ? CGSizeMake(50, 50) : CGSizeMake(27, 26);
        if (i == 2) {
            TarBarButton.frame = CGRectMake(x_spacing/2 + (i*(60+x_spacing)) , -24, 60, 60);
        } else {
            TarBarButton.frame = CGRectMake(x_spacing/2 + (i*(60+x_spacing)) , 0, 60, 60);
        }
        TarBarButton.tag = i + 1000;
        if (btnModel.title) {
            [TarBarButton setTitle:btnModel.title forState:(UIControlStateNormal)];
        }
        [TarBarButton addTarget:self action:@selector(didSelectIndexBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:TarBarButton];
    }
}

- (void)didSelectIndexBtnAction:(UIButton *)sender {
    
    if([self.delegate respondsToSelector:@selector(baseTabBarWillDeselectIndex:)]) {
        [self.delegate baseTabBarWillDeselectIndex:self.currentIndex];
    }
    self.currentIndex = sender.tag - 1000;
    if (self.delegate && [self.delegate respondsToSelector:@selector(baseTabBarDidSelectIndex:)]) {
        [self.delegate baseTabBarDidSelectIndex:sender.tag - 1000];
    }

}

//设置当前的下标
- (void)setCurrentIndex:(NSInteger)currentIndex {
    
    _currentIndex = currentIndex;
    
    for (UIView *view in self.subviews) {
        if (view.tag >= 1000 && view.tag < self.modelArray.count + 1000) {
            BaseTabBarModel *btnModel = self.modelArray[view.tag - 1000];
            BaseTabBarButton * btn = (BaseTabBarButton *)view;
            if (view.tag-1000 == currentIndex) {
                [btn setImage:[[UIImage imageNamed:btnModel.selectedImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
                [btn setImage:[[UIImage imageNamed:btnModel.selectedImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateHighlighted)];
                [btn setTitleColor:COLOR_TOPIC_SELECT forState:UIControlStateNormal];
                [btn setTitleColor:COLOR_TOPIC_SELECT forState:UIControlStateSelected];
            }else {
                [btn setImage:[[UIImage imageNamed:btnModel.defultImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
                [btn setImage:[[UIImage imageNamed:btnModel.defultImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateHighlighted)];
                [btn setTitleColor:COLOR_NOT_SELECT forState:UIControlStateNormal];
                [btn setTitleColor:COLOR_NOT_SELECT forState:UIControlStateSelected];
            }
        }
    }
    
}

@end
